var client = {};
var app = getApp();

// 获取选房告知书接口
client.getHouseNotice = function (that) {
  my.request({
    url: app.globalData.url + "506f294f-f5be-4489-8a34-e6903a38a555",
    method: "POST",
    headers: {
      "Access-Token": app.globalData.token
    },
    dataType: "json",
    success: function (res) {
      if (res.data.succeed == 0) {
        that.setData({
          noteText: res.data.data.noticeContent
        });
      } else {
        app.errorHandling("获取选" + my.getStorageSync({ key: 'taskType' }).data + "告知书失败：", res);
      }
    },
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () { }
  });
};

// 获取授权协议书验证码接口
client.getAgreementCode = function (that) {
  // 防止连续点击
  if (!that.data.secondClick) {
    return;
  }
  that.setData({
    secondClick: false
  });
  my.showLoading({
    content: '加载中...'
  });
  my.request({
    url: app.globalData.url + "b9076b89-6c28-4cdd-afc8-1957badc709c",
    method: "POST",
    headers: {
      "Access-Token": app.globalData.token
    },
    dataType: "json",
    success: function (res) {
      if (res.data.succeed == 0) {
        // 如果签署过电子协议书，则直接进入选房大厅页面
        if (res.data.errorMsg == "已签名成功") {
          my.redirectTo({
            url: "../mainHall/mainHall"
          });
        } else {
          my.showToast({
            content: "发送验证码成功",
            duration: 2000
          });
        }
      } else {
        app.errorHandling("发送验证码失败：", res);
      }
    },
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () {
      my.hideLoading();
      that.setData({
        secondClick: true
      });
    }
  });
};

// 提交授权协议书接口
client.submitAgreement = function (codeInputValue, that) {
  // 防止连续点击
  if (!that.data.secondClick) {
    return;
  }
  that.setData({
    secondClick: false
  });
  my.showLoading({
    content: '加载中...'
  });
  my.request({
    url: app.globalData.url + "b9076b89-6c28-4cdd-afc8-1957badc709c",
    method: "POST",
    data: {
      verifyCode: codeInputValue
    },
    headers: {
      "Access-Token": app.globalData.token
    },
    dataType: "json",
    success: function (res) {
      if (res.data.succeed == 0) {
        my.showToast({
          content: "提交授权协议书成功",
          duration: 2000
        });
        my.redirectTo({
          url: "../mainHall/mainHall"
        });
      } else {
        app.errorHandling("提交授权协议书失败：", res);
      }
    },
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () {
      my.hideLoading();
      that.setData({
        secondClick: true
      });
    }
  });
};

// 楼盘详情页的倒计时插件
client.countDown = function (seconds, that) {
  var noteId = that.data.lastNoteId + 1;
  that.setData({
    noteYesText: "（" + seconds + "s）",
    isCanNote: false,
    isCanGetCode: false
  });
  var interval = setInterval(function () {
    // 如果关闭对话框，则当前的和之前的所有倒计时全部结束
    if (that.data.lastNoteId >= noteId) {
      clearInterval(interval);
    }
    if (seconds > 0) {
      seconds--;
      that.setData({
        noteYesText: "(" + seconds + "s)"
      });
      console.info(
        that.data.lastNoteId + " 进入 " + noteId + " 倒计时 " + seconds
      );
    } else {
      that.setData({
        noteYesText: "",
        isCanNote: true,
        isCanGetCode: true,
        buttonBackground: "rgba(38,128,235,1)"
      });
      clearInterval(interval);
    }
  }, 1000);
};

module.exports = {
  client: client
};